<?php

class CommodityRequireComment_Model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $row
     * @return CommodityRequireComment
     */
    public static function getCommodityRequireCommentByRow($row)
    {
        $thatCommodityRequireComment = new CommodityRequireComment;
        $thatCommodityRequireComment->setId($row->id);
        $thatCommodityRequireComment->setContent($row->content);
        $thatCommodityRequireComment->setPrivate($row->private);
        $thatCommodityRequireComment->setReleaseTime($row->release_time);
        $thatCommodityRequireComment->setFatherId($row->father_id);
        $thatCommodityRequireComment->setUserId($row->user_id);
        $thatCommodityRequireComment->setCommodityRequireId($row->commodity_require_id);
        $thatCommodityRequireComment->setToUserId($row->to_user_id);
        $thatCommodityRequireComment->setMasterUserId($row->master_user_id);
        return $thatCommodityRequireComment;
    }

    /**
     * @param CommodityRequireComment $thatCommodityRequireComment
     * @return array
     */
    public static function getModelByCommodityRequireComment($thatCommodityRequireComment)
    {
        return array(
            'id'                   => $thatCommodityRequireComment->getId(),
            'content'              => $thatCommodityRequireComment->getContent(),
            'private'              => $thatCommodityRequireComment->getPrivate(),
            'release_time'         => $thatCommodityRequireComment->getReleaseTime(),
            'father_id'            => $thatCommodityRequireComment->getFatherId(),
            'user_id'              => $thatCommodityRequireComment->getUserId(),
            'commodity_require_id' => $thatCommodityRequireComment->getCommodityRequireId(),
            'to_user_id'           => $thatCommodityRequireComment->getToUserId(),
            'master_user_id'       => $thatCommodityRequireComment->getMasterUserId(),
        );
    }

    /**
     * @param int $id
     * @return CommodityRequireComment
     */
    public static function getCommodityRequireCommentById($id)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_require_comment.*');
        $CI->db->from('commodity_require_comment');
        $CI->db->where('commodity_require_comment.id', $id);

        $query = $CI->db->get();
        $row = $query->row();
        if ($row)
            return self::getCommodityRequireCommentByRow($row);
        else
            return null;
    }

    /**
     * @param int $commodity_require_id
     * @param int|bool $limit
     * @param int|bool $offset
     * @return array
     */
    public static function getCommodityRequireCommentArrayByCommodityRequireId($commodity_require_id, $limit = false, $offset = false)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_require_comment.*');
        $CI->db->from('commodity_require_comment');
        $CI->db->where('commodity_require_comment.commodity_require_id', $commodity_require_id);

        if ($limit !== false && is_numeric($limit)) {
            if ($offset === false || !is_numeric($offset))
                $offset = 0;
            $CI->db->limit($limit, $offset);
        }

        $query = $CI->db->get();
        $result = $query->result();

        $array = array();
        foreach ($result as $row) {
            $array[] = self::getCommodityRequireCommentByRow($row);
        }
        return $array;
    }

    /**
     * @param int $userId
     * @param int|bool $limit
     * @param int|bool $offset
     * @param string $orderBy
     * @param string $orderType
     * @return array
     */
    public static function getCommodityRequireCommentArrayByUserId($userId, $limit = false, $offset = false, $orderBy = 'id', $orderType = 'desc')
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_require_comment.*');
        $CI->db->from('commodity_require_comment');
        $CI->db->where('commodity_require_comment.user_id', $userId);
        $CI->db->order_by($orderBy, $orderType);

        if ($limit !== false && is_numeric($limit)) {
            if ($offset === false || !is_numeric($offset))
                $offset = 0;
            $CI->db->limit($limit, $offset);
        }

        $query = $CI->db->get();
        $result = $query->result();

        $array = array();
        foreach ($result as $row) {
            $array[] = self::getCommodityRequireCommentByRow($row);
        }
        return $array;
    }

    /**
     * @param int $userId
     * @param int|bool $limit
     * @param int|bool $offset
     * @param string $orderBy
     * @param string $orderType
     * @return array
     */
    public static function getCommodityCommentArrayByToUserIdOrMasterUserId($userId, $limit = false, $offset = false, $orderBy = 'id', $orderType = 'desc')
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_require_comment.*');
        $CI->db->from('commodity_require_comment');
        $CI->db->where('commodity_require_comment.user_id', $userId);
        $CI->db->or_where('commodity_require_comment.master_user_id', $userId);
        $CI->db->order_by($orderBy, $orderType);

        if ($limit !== false && is_numeric($limit)) {
            if ($offset === false || !is_numeric($offset))
                $offset = 0;
            $CI->db->limit($limit, $offset);
        }

        $query = $CI->db->get();
        $result = $query->result();

        $array = array();
        foreach ($result as $row) {
            $array[] = self::getCommodityRequireCommentByRow($row);
        }
        return $array;
    }

    /**
     * @param int $commodity_require_id
     * @return int
     */
    public static function countCommodityRequireCommentByCommodityRequireId($commodity_require_id)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_require_comment.commodity_id');
        $CI->db->from('commodity_require_comment');
        $CI->db->where('commodity_require_comment.commodity_require_id', $commodity_require_id);

        return $CI->db->count_all_results();
    }

    /**
     * @param int $UserId
     * @return int
     */
    public static function countCommodityRequireCommentByToUserIdOrMasterUserIdWithoutDeleted($UserId)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_require_comment.commodity_id');
        $CI->db->from('commodity_require_comment');
        $CI->db->where("(to_user_id = $UserId AND private < 100) OR (master_user_id = $UserId AND private < 100)");
        $CI->db->distinct();
        return $CI->db->count_all_results();
    }

    /**
     * @param CommodityRequireComment $thatCommodityRequireComment
     * @throws DatabaseException
     * @return CommodityRequireComment
     */
    public static function add($thatCommodityRequireComment)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $result = $CI->db->insert('commodity_require_comment', self::getModelByCommodityRequireComment($thatCommodityRequireComment));
        if ($result)
            return self::getCommodityRequireCommentById($CI->db->insert_id());
        else
            throw new DatabaseException('插入数据失败');
    }

    /**
     * @param CommodityRequireComment $thatCommodityRequireComment
     * @throws DatabaseException
     * @return CommodityRequireComment
     */
    public static function update($thatCommodityRequireComment)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('id', $thatCommodityRequireComment->getID());
        $result = $CI->db->update('commodity_require_comment', self::getModelByCommodityRequireComment($thatCommodityRequireComment));
        if ($result)
            return self::getCommodityRequireCommentById($thatCommodityRequireComment->getID());
        else
            throw new DatabaseException('更新数据失败');
    }

    /**
     * @param CommodityRequireComment $thatCommodityRequireComment
     * @throws DatabaseException
     * @return bool
     */
    public static function delete($thatCommodityRequireComment)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('id', $thatCommodityRequireComment->getID());
        $result = $CI->db->delete('commodity_require_comment');
        if ($result)
            return true;
        else
            throw new DatabaseException('删除数据失败');
    }

}
